import 'package:permission_handler/permission_handler.dart';

///请求权限
class PermissionUtils {
  //存储权限
  static Future<bool> checkStorage() async {
    PermissionStatus status = await Permission.storage.status;
    if (status.isGranted) {
      print('用户授权存储权限');
      //权限通过
      return true;
    } else if (status.isDenied) {
      print('用户拒绝存储权限');
      //权限通过
      return false;
    } /*else if (status.isUndetermined) {
      print('未申请存储权限');
      //权限通过
      return false;
    }*/ else {
      print('存储权限发生未知错误!');
      return false;
    }
  }

  static requestStorage() async {
    PermissionStatus status = await Permission.storage.request();
    if (status.isGranted) {
      print('用户授权存储权限');
      //权限通过
    } else if (status.isDenied) {
      print('用户拒绝存储权限');
      //权限通过
    }/* else if (status.isUndetermined) {
      print('未申请存储权限');
      //权限通过
    }*/ else {
      print('存储权限发生未知错误!');
    }
  }

  static checkCamera() async {
    PermissionStatus status1 = await Permission.camera.status;
    print(
        'camera: isGranted:${status1.isGranted}, isDenied:${status1.isDenied}');
    PermissionStatus status2 = await Permission.contacts.status;
    print(
        'contacts: isGranted:${status2.isGranted}, isDenied:${status2.isDenied}');
  }

  static requestCamera() async {
    List<Permission> permissions = [Permission.camera, Permission.contacts];
    Map<Permission, PermissionStatus> map = await permissions.request();
    map.forEach((key, value) {
      print(
          'permission: ${key.value}, isGranted:${value.isGranted}, isDenied:${value.isDenied}');
    });
  }
}
